package com.gjy.pd.prototype;

import lombok.Data;

import java.io.*;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-12-15 11:24:09
 */
@Data
public class Clone1 implements Serializable, Cloneable {

    private String name;

    @Override
    public Clone1 clone() {
        try {
            return (Clone1) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }

    public Clone1 deepClone() {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(baos);

             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
             ObjectInputStream ois = new ObjectInputStream(bais)
        ) {
            oos.writeObject(this);
            return ((Clone1) ois.readObject());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
